package com.cn.data.sync;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.client.AsyncRestTemplate;


/**
 * @Author: chenl
 * @Date: 2019/3/27 22:54
 */
@Component
@Slf4j
public class SyncTemplate {

    private AsyncRestTemplate asyncRestTemplate;

    @Autowired
    public SyncTemplate(AsyncRestTemplate asyncRestTemplate){
        this.asyncRestTemplate = asyncRestTemplate;
    }

    public String demo1() {
        String url = "http://localhost:8080/student/1";
        log.info("Start");
        ListenableFuture<ResponseEntity<String>> entity = asyncRestTemplate.getForEntity(url, String.class);
        entity.addCallback(new SuccessCallback<ResponseEntity<String>>() {
            @Override
            public void onSuccess(ResponseEntity<String> result) {
                log.info("A");
            }
        }, new FailureCallback() {
            @Override
            public void onFailure(Throwable ex) {
                log.info("B");
            }
        });
        log.info("C");
        return "End";
    }
}
